package com.sl.au.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.ChanceDelivery;
import com.sl.au.entity.ChanceManagerDelivery;
import com.sl.au.entity.SaleArea;
import com.sl.au.entity.User;
import com.sl.au.entity.Chance;

@Transactional(readOnly = true)
public interface ChanceManagerDeliveryRepository extends JpaRepository<ChanceManagerDelivery, String>{

	 /**
	  * 获取分配员工弹窗的数据
	  * @param chanceId
	  * @param pageable
	  * @return
	  */
	 @Query("select c from ChanceManagerDelivery c where c.saleChance.id =?1 "
	 		+ "and c.user.saleArea.id =?2 and c.targetType =0")
	 Page<ChanceManagerDelivery> findModuleUser(String chanceId, String areaId,Pageable pageable);
	 @Query("select count(*) from ChanceManagerDelivery c where c.saleChance.id =?1 "
			 + "and c.user.saleArea.id =?2 and c.targetType =0")
	 Long getModuleUserCount(String chanceId, String areaId);
	 /**
	  * 获取信息接收页面数据
	  * @param userId
	  * @param pageable
	  * @return
	  */
	 @Query("select c from ChanceManagerDelivery c where c.user.id =?1 and c.isReceived =?2 and (c.saleChance.name like '%'+?3+'%' "
			+ "or c.saleChance.source like '%'+?3+'%' or c.saleChance.sourceUnit like '%'+?3+'%' "
			+ "or c.saleChance.orderUnit like '%'+?3+'%' or c.saleChance.orderPerson like '%'+?3+'%'"
			+ "or c.saleChance.creator like '%'+?3+'%')")
	 Page<ChanceManagerDelivery> findAllChance(String userId,int isReceived,String key,Pageable pageable);
	 /**
	     * 获取信息接收页面数据
	     * @param userSuperior
	     * @return
	     */
	 @Query(" select count(*) from ChanceManagerDelivery c where c.user.id =?1 and c.isReceived =?2 and (c.saleChance.name like '%'+?3+'%' "
			+ "or c.saleChance.source like '%'+?3+'%' or c.saleChance.sourceUnit like '%'+?3+'%' "
			+ "or c.saleChance.orderUnit like '%'+?3+'%' or c.saleChance.orderPerson like '%'+?3+'%'"
			+ "or c.saleChance.creator like '%'+?3+'%')")
	 Long getAllChanceCount(String userId,int isReceived,String key);
}
